﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;

namespace MHWPA
{
    public partial class AlbumDetail : PhoneApplicationPage
    {
        System.Windows.Threading.DispatcherTimer dpt = new System.Windows.Threading.DispatcherTimer();
        Song selectedSong;
        TimeSpan duration;

        public AlbumDetail()
        {
            InitializeComponent();

            dpt.Interval = new TimeSpan(0, 0, 1);
            dpt.Tick += new EventHandler(dpt_Tick);

            this.DataContext = App.AlbumModel;
            App.AlbumModel.RefreshUri();
        }

        private void AlbumItemsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            AlbumDetailModel.SongItem selected = this.AlbumItemsListBox.SelectedItem as AlbumDetailModel.SongItem;

            FrameworkDispatcher.Update();
            selectedSong = Song.FromUri("Test", new Uri(selected.Audio));
            duration = TimeSpan.FromSeconds(Convert.ToDouble(selected.Duration));
            ProgressBar.Maximum = duration.Ticks;
            MediaPlayer.Play(selectedSong);
            dpt.Start();
            //Progress.Text = Microsoft.Xna.Framework.Media.MediaPlayer.PlayPosition.Ticks.ToString();

            //AudioPlayer.Source = new Uri(selected.Audio);
            //AudioPlayer.Play();

            /*
            if (selected == null) return;
            NavigationService.Navigate(new Uri("/Views/AlbumDetail.xaml", UriKind.Relative));

            ((ListBox)sender).SelectedItem = null;
             */
        }

        private void dpt_Tick(object sender, EventArgs e)
        {
            ProgressBar.Value = MediaPlayer.PlayPosition.Ticks;
            //Progress.Text = MediaPlayer.PlayPosition.Seconds.ToString();
            Progress.Text = string.Format("{0:D2}:{1:D2}:{2:D2}", MediaPlayer.PlayPosition.Hours, MediaPlayer.PlayPosition.Minutes, MediaPlayer.PlayPosition.Seconds);
            long remainingDuration = duration.Ticks - MediaPlayer.PlayPosition.Ticks;
            TimeSpan remainingD = TimeSpan.FromTicks(remainingDuration);
            //long remainingDuration = Convert.ToDouble(selected.Duration) - MediaPlayer.PlayPosition.Seconds;
            Remaining.Text = string.Format("{0:D2}:{1:D2}:{2:D2}", remainingD.Hours, remainingD.Minutes, remainingD.Seconds);
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (MediaPlayer.State == MediaState.Playing)
            {
                PlayPause.Content = "Pause";
            }
            else
            {
                PlayPause.Content = "Play";
            }

            AudioControls.Visibility = Visibility.Visible;
        }

        private void PlayPause_Click(object sender, RoutedEventArgs e)
        {
            if (MediaPlayer.State == MediaState.Playing)
            {
                MediaPlayer.Pause();
            }
            else
            {
                MediaPlayer.Resume();
            }
            AudioControls.Visibility = Visibility.Collapsed;
        }
    }
}